﻿using System;
using System.Collections.Generic;
using System.Globalization;

using zijian666.AnyExtensions.Privates;

namespace zijian666.AnyExtensions;
/// <summary>
/// 区域类型的扩展方法
/// </summary>
public static class CultureInfoExtensions
{
    /// <summary>
    /// 获取字符串比较器
    /// </summary>
    /// <param name="culture">区域信息</param>
    /// <param name="options">比较选项</param>
    public static StringComparer GetStringComparer(this CultureInfo? culture, CompareOptions options)
    {
        culture ??= CultureInfo.CurrentCulture;
        return options switch
        {
            CompareOptions.None or CompareOptions.Ordinal => StringComparer.Ordinal,
            CompareOptions.OrdinalIgnoreCase => StringComparer.OrdinalIgnoreCase,
            CompareOptions.IgnoreCase => StringComparer.CurrentCultureIgnoreCase,
            _ => StringComparerFactory.Create(options, culture),
        };
    }

    /// <summary>
    /// 以指定比较器创建字典
    /// </summary>
    /// <param name="culture">区域信息</param>
    /// <param name="options">比较选项</param>
    public static Dictionary<string, T> CreateDictionary<T>(this CultureInfo? culture, CompareOptions options)
        => new(culture.GetStringComparer(options));


    /// <summary>
    /// 以指定比较器创建哈希集合 <seealso cref="HashSet{string}"/>
    /// </summary>
    /// <param name="culture">区域信息</param>
    /// <param name="options">比较选项</param>
    public static HashSet<string> CreateHashSet(this CultureInfo? culture, CompareOptions options)
        => new(culture.GetStringComparer(options));
}
